// 多属性响应

const targetMap = new WeakMap();
function track(target, key) {
  let depsMap = targetMap.get(target);
  if (!depsMap) {
    targetMap.set(target, (depsMap = new Map()));
  }
  let dep = depsMap.get(key);
  if (!dep) {
    depsMap.set(key, (dep = new Set()));
  }
  dep.add(effect);
}
function trigger(target, key) {
  const depsMap = targetMap.get(target);
  if (!depsMap) {
    return;
  }
  let dep = depsMap.get(key);
  if (dep) {
    dep.forEach((effect) => effect());
  }
}

// test
let product = { price: 5, quantity: 2 };
let user = { firstName: 'Jack', lastName: 'Lee' };
let effect = () => {
  total = product.price * product.quantity;
};
track(product, 'quantity');
effect();
console.log(total); // 10

// update
product.quantity = 5;
trigger(product, 'quantity');
console.log(total); //25
